﻿using System;
using Microsoft.Extensions.Configuration;

namespace AFei.PW_Manager.Config
{
    public class ConfigHelper
    {
        private static IConfiguration _iConfiguration;
        public ConfigHelper(IConfiguration configuration)
        {
            _iConfiguration = configuration;
        }

        public static void Init(IConfiguration configuration)
        {
            _iConfiguration = configuration;
        }

        public static string ConnectionType => _iConfiguration["ConnectionType"];

        public static string GetAppConfigByKey(string option)
        {
            if (string.IsNullOrEmpty(option))
            {
                throw new NullReferenceException();
            }
            return _iConfiguration[option];
        }

        public static T GetAppConfigByKey<T>(string option)
        {
            if (string.IsNullOrEmpty(option))
            {
                throw new NullReferenceException();
            }

            return (T) Convert.ChangeType(_iConfiguration[option], typeof(T));
        }
    }
}
